﻿using System;
using System.Collections;
using System.IO;
using System.Linq;
using System.Text;
using argusThreeAndAHalfMen;

namespace DataParser
{
    public class Parser
    {//parser class for reading and writing data
        public static void SaveBranchList(string dataFileLocation)
        {//writes branchlist data model in data file format
            using (var outData = new StreamWriter(dataFileLocation)) //allows automatic closing of streamwriter when it ceases use
            {
                foreach (Branch branch in frmHome.branchList)
                {
                    outData.WriteLine(branch.BranchID);
                    outData.WriteLine(branch.Nickname);
                    outData.WriteLine(branch.StreetNumber);
                    outData.WriteLine(branch.StreetName);
                    outData.WriteLine(branch.City);
                    outData.WriteLine(branch.County);
                    outData.WriteLine(branch.Postcode);

                    for (int i = 0; i < 3; i++)
                        outData.WriteLine(branch.NearbyBranchIds[i]);

                    outData.WriteLine(branch.Categories.Count);

                    foreach (Category category in branch.Categories)
                    {
                        outData.WriteLine(category.CategoryID);
                        outData.WriteLine(category.CategoryName);
                        outData.WriteLine(category.ProductList.Count);

                        foreach (Product product in category.ProductList)
                        {
                            outData.WriteLine(product.ProductID);
                            outData.WriteLine(product.ProductName);
                            outData.WriteLine(product.ProductDesc);
                            outData.WriteLine(product.ProductImagePath);
                            outData.WriteLine(product.ProductPrice);
                            outData.WriteLine(product.ProductStockLevel);
                        }
                    }

                }
            }
        }

        public ArrayList ParseBranch(string dataFileLocation)
        {
            var branchList = new ArrayList();

            using (var inData = new StreamReader(dataFileLocation)) //allows automatic closing of streamreader when it ceases use
            {
                while (!inData.EndOfStream)
                {//create and add branch to branchlist
                    var branch = Branch(inData);

                    if (branch != null)
                        branchList.Add(branch);
                }
            }

            //create objects for nearby branches and add them to an arraylist for later use
            foreach (Branch branch in branchList)
            {
                foreach (int branchID in branch.NearbyBranchIds)
                {
                    foreach (Branch nextBranch in branchList)
                    {
                        if (branch == nextBranch)
                            continue;

                        if (nextBranch.BranchID == branchID)
                        {
                            branch.NearbyBranches.Add(nextBranch);
                            continue;
                        }
                    }
                }
            }

            return branchList;
        }

        private Branch Branch(StreamReader inData)
        {//reads data into branch object using streamreader
            var branch = new Branch();
            
            branch.BranchID = Convert.ToInt32(inData.ReadLine());
            branch.Nickname = inData.ReadLine();
            branch.StreetNumber = inData.ReadLine();
            branch.StreetName = inData.ReadLine();
            branch.City = inData.ReadLine();
            branch.County = inData.ReadLine();
            branch.Postcode = inData.ReadLine();

            branch.NearbyBranchIds.Add(Convert.ToInt32(inData.ReadLine()));
            branch.NearbyBranchIds.Add(Convert.ToInt32(inData.ReadLine()));
            branch.NearbyBranchIds.Add(Convert.ToInt32(inData.ReadLine()));

            int categories = Convert.ToInt32(inData.ReadLine());
            for (int i = 0; i < categories; i++)
            {//calls upon ParseCategory() using for loop to add categories into the branch object
                var category = ParseCategory(inData);

                if (category != null)
                    branch.Categories.Add(category);
            }

            return branch;
        }

        private Category ParseCategory(StreamReader reader)
        {//creates and returns category object using streamreader
            var category = new Category();

            category.CategoryID = Convert.ToInt32(reader.ReadLine());
            category.CategoryName = reader.ReadLine();

            int numProducts = Convert.ToInt32(reader.ReadLine());
            for (int i = 0; i < numProducts; i++)
            {//calls upon ParseProduct() using for loop to add correct number of products into category object
                var product = ParseProduct(reader);

                if (product != null)
                    category.ProductList.Add(product);
            }

            return category;
        }

        private Product ParseProduct(StreamReader reader)
        {//creates and returns product object using streamreader
            var product = new Product();

            product.ProductID = Convert.ToInt32(reader.ReadLine());
            product.ProductName = reader.ReadLine();
            product.ProductDesc = reader.ReadLine();
            product.ProductImagePath = reader.ReadLine();
            product.ProductPrice = Convert.ToSingle(reader.ReadLine());
            product.ProductStockLevel = Convert.ToInt32(reader.ReadLine());

            return product;
        }
    }
}
